package com.gxgeek.springbootsendmessage;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;

/**
 * @author gaoxing
 * @create 2017-12-08 下午3:13
 */
@Service
@Slf4j
public class SendService {
    @Value("${spring.mail.username}")
    private String from;

    @Autowired
    private JavaMailSender sender;
    @Autowired
    Configuration freemarkerConfiguration;

    /*发送邮件的方法 - 最简单的*/
    public void sendSimple(String to, String title, String content){
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from); //发送者
        message.setTo(to); //接受者
        message.setSubject(title); //发送标题
        message.setText(content);  //发送内容
        sender.send(message);

        log.info("邮件发送成功");

    }
//    @Recover
    @Retryable
    public void sendAttachmentsMail(String to, String title, String content) throws MessagingException {
        MimeMessage mimeMessage = sender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(title);
        helper.setText(content);

        FileSystemResource file = new FileSystemResource(new File("/Users/gaoxing/Downloads/u=938116244,3259922549&fm=27&gp=0.jpg"));
        helper.addAttachment("附件-1.jpg", file);
        helper.addAttachment("附件-2.jpg", file);

        sender.send(mimeMessage);
        log.info("邮件发送成功");

    }
    public void sendInlineMail(String to, String title, String content) throws MessagingException {
        FileSystemResource file = new FileSystemResource(new File("/Users/gaoxing/Downloads/u=938116244,3259922549&fm=27&gp=0.jpg"));


        MimeMessage mimeMessage = sender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(title);

        //这里需要注意的是addInline函数中资源名称weixin需要与正文中cid:weixin对应起来
        helper.setText("<html><body><img src=\"cid:weixin\" ></body></html>", true);
        helper.addInline("weixin", file);

        sender.send(mimeMessage);
        log.info("邮件发送成功");

    }
    public void sendTemplateMail(String to, String title, String content) throws MessagingException, IOException, TemplateException {
        MimeMessage mimeMessage = sender.createMimeMessage();

        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        helper.setFrom(from);
        helper.setTo(to);
        helper.setSubject(title);
        Map<String, Object> model = new HashMap();
        model.put("username", "gxgeek");
        String text = FreeMarkerTemplateUtils.processTemplateIntoString( freemarkerConfiguration.getTemplate("template.ftl"),  model);
        helper.setText(text, true);
        sender.send(mimeMessage);
        log.info("邮件发送成功");

    }

    public static void main(String[] args) throws IOException, TemplateException {

        Map model = new HashMap();
        model.put("age", 101);
        model.put("username", "gxgeek");
//        String templateString = "[#if age > 100] hello:@{username} ${username} [/#if]";
        String templateString = "[#if age > 100] hello: ${username} [/#if]";

        Template t = newSyntaxFreemarkerTemplate(new StringReader(templateString),"@{");
        String result = FreeMarkerTemplateUtils.processTemplateIntoString(t, model);
        System.out.println(result);
        //out: hello: gxgeek

    }

    public static Template newSyntaxFreemarkerTemplate(Reader reader, String newExpressionSyntax) throws IOException {
        Configuration conf = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
        conf.setTagSyntax(Configuration.SQUARE_BRACKET_TAG_SYNTAX);

//        String templateString = IOUtils.toString(reader);
//        templateString = StringUtils.replace("[#assign doller='$']"+templateString, "${", "${doller}{");
//        templateString = StringUtils.replace(templateString, "${", "${'$'}{");
//        templateString = StringUtils.replace(templateString, "@{", "${");


//        Template t = new Template("A",new StringReader(templateString),conf);
        Template t = new Template("A",reader,conf);
        return t;
    }
}

